﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.kiosk {
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class CraftProductCommand extends Command {

        public var event:InfoEvent;
        public var pendingModel:PendingModel;
        private var pendingVOs:Vector.<PendingItemVO>;

        public function CraftProductCommand(){
            this.pendingVOs = new Vector.<PendingItemVO>();
            super();
        }
        override public function execute():void{
            var _local1:Object = this.event.info;
            this.createPendingVOs(_local1.required_items);
            this.addPendingVOs();
            var _local2:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, this.onResult);
            _local2.addRequest("craft_product", {id:this.event.id, product:_local1.code});
            dispatch(_local2);
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Object;
            if (_arg1.success){
                _local2 = _arg1.data.craft_product;
                this.updateChanges(_local2);
                this.updateItems(_local2.items);
                this.track();
            };
            this.removePendingVOs();
        }
        private function createPendingVOs(_arg1:Object):void{
            var _local2:Object;
            for each (_local2 in _arg1) {
                this.pendingVOs.push(new PendingItemVO(_local2.type, _local2.amount));
            };
        }
        private function addPendingVOs():void{
            var _local1:PendingItemVO;
            for each (_local1 in this.pendingVOs) {
                this.pendingModel.addPendingItem(_local1);
            };
        }
        private function removePendingVOs():void{
            var _local1:PendingItemVO;
            for each (_local1 in this.pendingVOs) {
                this.pendingModel.removePendingItem(_local1);
            };
            this.pendingVOs = null;
        }
        private function track():void{
            var _local1:Object = {type:"crafting", item_bought:this.event.info};
            dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.KIOSK, _local1));
        }
        private function updateItems(_arg1:Object):void{
            dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ICONS, this.event.id, _arg1));
        }
        private function updateChanges(_arg1:Object):void{
            dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, _arg1));
        }

    }
}//package net.wooga.pets.controllers.kiosk 
